$(function () {
	$('.summernote').summernote({
		height: 215,                  
        lang: 'zh-CN'
    });
    $("#jqGrid").jqGrid({
        url: '../qygj/visamaterial/list',
        datatype: "json",
        colModel: [			
			//{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '产品名称', name: 'productName', index: 'product_name', width: 80 }, 			
			{ label: '人员类型', name: 'personTypeName', index: 'person_type_name', width: 60 }, 			
			{ label: '资料', name: 'label', index: 'label', width: 60 }, 			
			{ label: '资料内容', name: 'content', index: 'content', width: 200 }, 			
			//{ label: '附件类型：0模板1样例', name: 'fileType', index: 'file_type', width: 80 }, 			
			{ label: '资料文件', name: 'fileName', index: 'file_name', width: 80 ,sortable:false} 			
			//{ label: '注意事项', name: 'notice', index: 'notice', width: 80 }, 			
			//{ label: '备注', name: 'remark', index: 'remark', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    vm.loadProduct();
    vm.loadPersonType();
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		q:{
			key: null
		},
		product:[],
		personType : [],
		visaMaterial: {
			fileName : null
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.visaMaterial = {fileName : null};
			$('.summernote').code('')
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.visaMaterial.id == null ? "../qygj/visamaterial/save" : "../qygj/visamaterial/update";
			vm.visaMaterial.content = $('.summernote').code()
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.visaMaterial),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../qygj/visamaterial/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../qygj/visamaterial/info/"+id, function(r){
                vm.visaMaterial = r.visaMaterial;
                console.dir(vm.visaMaterial);
                $('.summernote').code(vm.visaMaterial.content);
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
				postData:{'key': vm.q.key},
                page:page
            }).trigger("reloadGrid");
		},
		loadProduct : function() {
			$.ajax({
				type: "POST",
			    url: "../qygj/product/queryAll",
                contentType: "application/json",
			    success: function(r){
					if(r.code == 0){
						vm.product = r.product;
					}
				}
			});
		},
		loadPersonType : function() {
			$.ajax({
				type: "POST",
			    url: "../qygj/persontype/queryAll",
                contentType: "application/json",
			    success: function(r){
					if(r.code == 0){
						vm.personType = r.personType;
					}
				}
			});
		},
		showFileSel : function(event) {
			showFile(null, vm.setFile);
		},
		setFile : function(name, url) {
			vm.visaMaterial.fileName = name;
		    vm.visaMaterial.filePath = url;
		}
	}
});